package cn.hsw.component.web.exception;

import cn.hsw.component.core.dto.ResultDto;
import cn.hsw.component.core.enums.HttpEnum;
import cn.hsw.component.core.util.ResultUtil;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class BaseErrorController implements ErrorController {

    private final static String ERROR_PATH = "/error";


    @RequestMapping(path = ERROR_PATH )
    public ResultDto error(HttpServletRequest request, HttpServletResponse response){

        if(response.getStatus() == HttpEnum.NOT_FOUND.getCode()){
            response.setStatus(200);
            return ResultUtil.error(HttpEnum.NOT_FOUND);
        }
        response.setStatus(200);
        return ResultUtil.error(HttpEnum.ERROR);

    }

    public String getErrorPath() {

        return ERROR_PATH;

    }
}
